﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.EntityFrameworkCore
{
    public interface IRepositoryBase<TEntity>
    {
        IQueryable<TEntity> Table { get; }
        IQueryable<TEntity> TableNoTracking { get; }

        List<TEntity> GetAllList();
        Task<List<TEntity>> GetAllListAsync();
        List<TEntity> GetList(Expression<Func<TEntity, bool>> predicate, bool isNoTracking = true);
        Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool isNoTracking = true);
        PageData<TEntity> GetPageList(Expression<Func<TEntity, bool>> predicate, int pageIndex, int pageSize, bool isNoTracking = true);
        Task<PageData<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> predicate, int pageIndex, int pageSize, bool isNoTracking = true);
        Task<PageData<T>> PagingAsync<T>(IQueryable<T> query, int pageIndex, int pageSize);
        TEntity GetFirstModel();
        Task<TEntity> GetFirstModelAsync();
        int GetAllCount();
        Task<int> GetAllCountAsync();
        int GetCount(Expression<Func<TEntity, bool>> predicate);
        Task<int> GetCountAsync(Expression<Func<TEntity, bool>> predicate);
        TEntity GetModel(object id, bool isNoTracking = false);
        Task<TEntity> GetModelAsync(object id, bool isNoTracking = false);
        bool Insert(TEntity entity);
        Task<bool> InsertAsync(TEntity entity);
        bool Update(TEntity entity);
        Task<bool> UpdateAsync(TEntity entity);
        bool Delete(object id);
        //Task<bool> DeleteAsync(TKey id);
        //bool SoftDelete(long id);
        //Task<bool> SoftDeleteAsync(long id);


    }
    public class PageData<T>
    {
        public IList<T> Items { get; set; }
        /// <summary>
        /// 页码，从0开始
        /// </summary>
        public int PageIndex { get; set; }
        public int ItemsCount { get; set; }
        public int PageSize { get; set; }
        public int PageCount
        {
            get
            {
                if (PageSize == 0)
                    return 0;
                return ItemsCount / PageSize + (ItemsCount % PageSize == 0 ? 0 : 1);
            }
        }
    }
}
